package com.cat.simulation;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/sum-of-even-numbers-after-queries/
 * @create 2025/8/28 18:47
 * @since JDK17
 */

public class Solution03 {
    public int[] sumEvenAfterQueries(int[] nums, int[][] queries) {
        int n = nums.length, m = queries.length, sum = 0;
        int[] ans = new int[m];
        for (int i = 0; i < n; i++) {
            if (Math.abs(nums[i]) % 2 == 0) {
                sum += nums[i];
            }
        }
        for (int i = 0; i < m; i++) {
            int val = queries[i][0], idx = queries[i][1];
            if (Math.abs(nums[idx]) % 2 == 0) {
                if (Math.abs(val) % 2 == 0) {
                    sum += val;
                } else {
                    sum -= nums[idx];
                }
            } else {
                if (Math.abs(val) % 2 == 1) {
                    sum += nums[idx] + val;
                }
            }
            ans[i] = sum;
            nums[idx] += val;
        }
        return ans;
    }
}
